

# 导入需要的包 库
import urllib.request

# 获取请求网址url
url = 'https://www.zhoujixsw.cc/books/142281/'

headers = {
    # 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    # # 'accept-encoding': 'gzip, deflate, br, zstd',
    # 'accept-language': 'zh-CN,zh;q=0.9',
    # 'cache-control': 'no-cache',
    # 'cookie': 'visittime=articlevisited',
    # 'pragma': 'no-cache',
    # 'priority': 'u=0, i',
    # 'referer': 'https://www.baidu.com/link?url=Fuk-eVarzBXRtD6FACSeOfZf5sCBIp-O7ZQYTl7iQVq_QpuEgfWhPlZIliwxk5ys&wd=&eqid=ac4752630012463c0000000366c466f5',
    # 'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
    # 'sec-ch-ua-mobile': '?0',
    # 'sec-ch-ua-platform': '"Windows"',
    # 'sec-fetch-dest': 'document',
    # 'sec-fetch-mode': 'navigate',
    # 'sec-fetch-site': 'cross-site',
    # 'sec-fetch-user': '?1',
    # 'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
}

# 定制请求头
request = urllib.request.Request(url=url, headers=headers)

# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(request)

# 获取服务器响应的数据
content = response.read().decode('utf-8')
# print(content)

# 获取想要的数据
from lxml import etree
tree = etree.HTML(content)

name_list = tree.xpath('//div[@class="all"]/ul/li/a/@title')

href_list = tree.xpath('//div[@class="all"]/ul/li/a/@href')

for i in range(10):
    print(name_list[i], href_list[i])

    url = f'{href_list[i]}'

    headers = headers

    request = urllib.request.Request(url=url, headers=headers)

    response = urllib.request.urlopen(request)

    content = response.read().decode('utf-8')

    # print(content)
    tree = etree.HTML(content)

    text = tree.xpath('//div[@id="booktxt"]/p/text()')
    # print(text)
    for j in text:
        # print(i, type(i))

        with open(f'./满朝文武/{name_list[i]}.txt', 'a', encoding='utf-8') as fp:
            fp.write(str(j)+'\n')


